Using the Custom XSLT Function |
The Custom XSLT function is used to embed custom XSLT in the data transformations. While you can use the functions and instructions provided by platform, you can also define your custom XSLT and use them in the data maps. However, you must ensure to follow the XSLT standards while defining the custom functions. Based on the XSLT provided, the target node structure will be created.
The following sections describe the usage of this Custom XSLT function in various scenarios:
- Access the Data Transformation Modeler (
) to create a data transformation model. The Data Transformation modeler page is displayed.
- Drag the source schema from the project tree to the Source pane of the Modeler tab. The schema instance of the Source is displayed on the modeler.
- Drag the target schema to the Target pane of the Modeler tab. The schema instance of the Target is displayed on the modeler.
- Right-click the Map Canvas(middle) pane of the Modeler tab and select Functions > Advanced > Custom XSLT. The Custom XSLT icon appears on the Map canvas.
- Click Custom XSLT. The properties table of the Custom XSLT is displayed at the bottom.
You must provide the Custom XSLT content along with the root nodexsl:template
as shown below:
<xsl:template xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Custom XSLT content is given here --> </xsl:template>
![]() |
|
The following scenarios demonstrate some of the usages of Custom XSLT through samples:
Scenario 1: Creating repeated target elements based on multiple source elements without linking from Source
Multiple source elements will be mapped to repeated target element.
Source Schema
<element name="Company" xmlns="http://www.w3.org/2001/XMLSchema"> <complexType> <sequence> <element maxOccurs="unbounded" name="Employee"> <complexType> <sequence> <element name="Mobile"> <complexType> <sequence> <element name="Home" type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema"/> <element name="Office" type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema"/> </sequence> </complexType> </element> </sequence> </complexType> </element> </sequence> </complexType> </element> |
Target Schema
<element xmlns="http://www.w3.org/2001/XMLSchema" name="Team"> <complexType> <sequence> <element name="Employee" maxOccurs="unbounded"> <complexType> <sequence> <element xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" type="xs:string" name="Mobile" maxOccurs="unbounded" /> </sequence> </complexType> </element> </sequence> </complexType> </element> </sequence> </complexType> </element> |
- Do the following to map the elements between the source and target templates using the Custom XSLT function:
- Select Custom XSLT and click Team\Employee\Mobile in the Target pane.
- Complete XPath of the source element should be given in the XSLT as no source context is selected.
- Double-click Custom XSLT icon to open the properties pane and provide the following XSLT.
<xsl:template xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <Mobile> <xsl:value-of select="/Company/Employee/Mobile/Home"/> </Mobile> <Mobile> <xsl:value-of select="/Company/Employee/Mobile/Office"/> </Mobile> </xsl:template>
- Click the Test Model tab. The Test Model tab with the source XML is displayed.
- In the Source XML pane, type appropriate values for the source elements. Alternatively, you may click Fill Source Data, which assigns sample string values to the source elements. Make changes to these values, if required. The elements in the Source XML pane are populated with the specified values.
- Click Transform to start the data transformation process.
The data is transformed and displayed in the Transformed XML pane indicating the completion of the process as shown below:
Test Source XML
<Company> <Employee> <Mobile> <Home>987654321</Home> <Office>987654367</Office> </Mobile> </Employee> </Company> |
Generated Target XML
<Team> <Employee> <Mobile>987654321</Mobile> <Mobile>987654367</Mobile> </Employee> </Team> |
Scenario 2: Creating an element under repeated target node linking from Source
Only one target element is created
Source Schema 2
<element name="Catalog" xmlns="http://www.w3.org/2001/XMLSchema"> <complexType> <sequence> <element name="Products"> <complexType> <sequence> <element maxOccurs="unbounded" minOccurs="0" name="Product"> <complexType> <sequence> <xsd:element name="ProductId" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/> <xsd:element name="ProductDescription" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/> </sequence> </complexType> </element> </sequence> </complexType> </element> </sequence> </complexType> </element> |
Target Schema 2
<element name="PurchaseOrder" xmlns="http://www.w3.org/2001/XMLSchema"> <complexType> <sequence> <element name="Items"> <complexType> <sequence> <element maxOccurs="unbounded" minOccurs="0" name="Item"> <complexType> <sequence> <xsd:element name="ItemCode" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/> <element name="ItemDescription" type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/> </sequence> </complexType> </element> </sequence> </complexType> </element> </sequence> </complexType> </element> |
- Do the following to map the elements between the source and target templates using the Custom XSLT function:
- Select Custom XSLT and click
PurchaseOrder/Items/Item/ItemCode
in the Target pane. - Obtain the complete XPath of the element
Catalog/Products/Product/ProductId
in the Source pane. - Double-click Custom XSLT icon to open the properties pane and provide following XSLT.
<xsl:template xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <ItemCode> <xsl:value-of select="/Catalog/Products/Product/ProductId"/> </ItemCode> </xsl:template>
- Select Custom XSLT and click
- Click the Test Model tab. The Test Model tab with the source XML is displayed.
- In the Source XML pane, type appropriate values for the source elements. Alternatively, you can click Fill Source Data, which assigns sample string values to the source elements. Make changes to these values, if required. The elements in the Source XML pane are populated with the specified values.
- Click Transform to start the data transformation process.
- The data is transformed and displayed in the Transformed XML pane indicating completion of the process.
Test Source XML
<Catalog> <Products> <Product> <ProductId>1010</ProductId> <ProductDescription>Funiture</ProductDescription> </Product> <Product> <ProductId>1020</ProductId> <ProductDescription>clothing</ProductDescription> </Product> </Products> </Catalog> |
Generated Target XML
<PurchaseOrder> <Items> <Item> <ItemCode>1010</ItemCode> </Item> </Items> </PurchaseOrder> |
Scenario 3: Creating repeated target elements when Source element is selected and an Absolute XPath is provided
This scenario is similar to scenario 2 with a link to the source element is provided additionally.
Consider the Source Schema and Target Schema that are given in Scenario 2.
- Do the following to map the elements between the source and target templates using the Custom XSLT function:
- Select Custom XSLT and click
PurchaseOrder/Items/Item/ItemCode
in the Target pane. - Select
Catalog/Products/Product/ProductId
from the Source pane and click Custom XSLT. - Get XPath of the source element
Catalog/Products/Product/ProductId
to prepare the XSLT.
- Select Custom XSLT and click
- Double-click the Custom XSLT icon to open the properties pane and provide the following XSLT:
<xsl:template xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <ItemCode> <xsl:value-of select="/Catalog/Products/Product/ProductId"/> </ItemCode> </xsl:template>
- Click the Test Model tab. The Test Model tab with the source XML is displayed.
- In the Source XML pane, type appropriate values for the source elements. Alternatively, you may click Fill Source Data , which assigns sample string values to the source elements. Make changes to these values, if required. The elements in the Source XML pane are populated with the specified values.
- Click Transform to start the data transformation process.
- The data is transformed and displayed in the Transformed XML pane indicating the completion of the process.
Test Source XML
<Catalog> <Products> <Product> <ProductId>1010</ProductId> <ProductDescription>Funiture</ProductDescription> </Product> <Product> <ProductId>1020</ProductId> <ProductDescription>clothing</ProductDescription> </Product> </Products> </Catalog> |
Generated Target XML
<PurchaseOrder> <Items> <Item> <ItemCode>1010</ItemCode> </Item> <Item> <ItemCode>1010</ItemCode> </Item> </Items> </PurchaseOrder> |
Scenario 4 : Creating repeated target elements when the Source element is not selected but a direct link is provided for other element
Consider the Source Schema and Target Schema that are used in Scenario 2.
- Do the following to map the elements between the source and target templates using the Custom XSLT function:
- Select
Catalog/Products/Product/ProductId
from Source pane and click onPurchaseOrder/Items/Item/ItemCode
in the Target pane. A direct mapping is created. - Select Custom XSLT and click
PurchaseOrder/Items/Item/ItemDescription
in the Target pane. Note: Ensure to provide the complete XPath of the source element in the XSLT as you do not link to the Source directly. - Double-click Custom XSLT icon to open the properties pane and provide following XSLT:
<xsl:template xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <ItemDescription> <xsl:value-of select="/Catalog/Products/Product/ProductDescription"/> </ItemDescription> </xsl:template>
- Select
- Click the Test Model tab. The Test Model tab with the source XML is displayed.
- In the Source XML pane, type appropriate values for the source elements. Alternatively, you may click Fill Source Data, which assigns sample string values to the source elements. Make changes to these values, if required. The elements in the Source XML pane are populated with the specified values.
- Click Transform to start the data transformation process.
- The data is transformed and displayed in the Transformed XML pane indicating the completion of the process. The transformed XML is similar to the transformed XML in Scenario 2.
Test Source XML
<Catalog> <Products> <Product> <ProductId>1010</ProductId> <ProductDescription>Furniture</ProductDescription> </Product> <Product> <ProductId>1020</ProductId> <ProductDescription>Clothing</ProductDescription> </Product> </Products> </Catalog> |
Generated Target XML
<PurchaseOrder> <Items> <Item> <ItemCode>1010</ItemCode> <ItemDescription>Furniture</ItemDescription> </Item> <Item> <ItemCode>1020</ItemCode> <ItemDescription>Furniture</ItemDescription> </Item> </Items> </PurchaseOrder> |
Scenario 5 : Creating repeated target elements when the Source element is selected and a relative XPath is provided
Consider the Source Schema and Target Schema that are used in Scenario 2.
- Do the following to map the elements between the source and target templates using the Custom XSLT function:
- Select
Catalog/Products/Product/ProductId
from Source pane and clickPurchaseOrder/Items/Item/ItemCode
in the Target pane. A direct mapping is created. - Select Custom XSLT and click
PurchaseOrder/Items/Item/ItemDescription
in the Target pane. - Select
Catalog/Products/Product/ProductDescription
and click Custom XSLT. The source element is now linked with the Custom XSLT. - In the XSLT, provide the XPath from the Source; that is, the relative path to the source element. The Source element is Product and the XPath is considered from there; that is, ProductDescription.
- Double-click Custom XSLT icon to open the properties pane and provide following XSLT:
<xsl:template xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <ItemDescription> <xsl:value-of select="ProductDescription"/> </ItemDescription> </xsl:template>
- Select
- Click the Test Model tab. The Test Model tab with the source XML is displayed.
- In the Source XML pane, type appropriate values for the source elements. Alternatively, you may click Fill Source Data, which assigns sample string values to the source elements. Make changes to these values, if required. The elements in the Source XML pane are populated with the specified values.
- Click Transform to start the data transformation process.
- The data is transformed and displayed in the Transformed XML pane indicating the completion of the process. The transformed XML is similar to the transformed XML in Scenario 2.
Test Source XML
<Catalog> <Products> <Product> <ProductId>1010</ProductId> <ProductDescription>Furniture</ProductDescription> </Product> <Product> <ProductId>1020</ProductId> <ProductDescription>Clothing</ProductDescription> </Product> </Products> </Catalog> |
Generated Target XML
<PurchaseOrder xmlns=""> <Items> <Item> <ItemCode>1010</ItemCode> <ItemDescription>Furniture</ItemDescription> </Item> <Item> <ItemCode>1020</ItemCode> <ItemDescription>Clothing</ItemDescription> </Item> </Items> </PurchaseOrder> |
Scenario 6 : Creating a Target element based on the Source elements value without the Source element selected directly
Consider the following Source schema and Target Schema for this Scenario.
Source Schema
<element name="Orders" xmlns="http://www.w3.org/2001/XMLSchema"> <complexType> <sequence> <element maxOccurs="unbounded" minOccurs="0" name="Order"> <complexType> <sequence> <xsd:element name="CustomerID" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/> <element maxOccurs="unbounded" minOccurs="0" name="date"> <complexType> <sequence> <xsd:element name="type" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/> <xsd:element name="value" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/> </sequence> </complexType> </element> </sequence> </complexType> </element> </sequence> </complexType> </element> |
Target Schema
<element name="PurchaseOrder" xmlns="http://www.w3.org/2001/XMLSchema"> <complexType> <sequence> <element name="OrderId" type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema"/> <element name="Dates"> <complexType> <sequence> <element minOccurs="0" name="OrderDate" type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/> <element minOccurs="0" name="BillingDate" type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/> <element minOccurs="0" name="DeliveryDate" type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/> </sequence> </complexType> </element> </sequence> </complexType> </element> |
- Do the following to map the elements between the source and target templates using the Custom XSLT function:
- Select Custom XSLT and click
/PurchaseOrder/Dates/OrderDate
in the Target pane. - Provide the XPath of the source elements in the XSLT as required to create target nodes.
Double-click Custom XSLT icon to open the properties pane and provide following XSLT: Note: The variable orderpath is created and used throughout the XSLT and xsl:if with a condition is used.
<xsl:template xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="orderpath" select="/Orders/Order"/> <OrderDate> <xsl:value-of select="$orderpath/date[type='Order']/value"/> </OrderDate> <xsl:if test="$orderpath/date[type='Billing']"> <BillingDate> <xsl:value-of select="$orderpath/date[type='Billing']/value"/> </BillingDate> </xsl:if> <xsl:if test="$orderpath/date[type='Delivery']"> <DeliveryDate> <xsl:value-of select="$orderpath/date[type='Delivery']/value"/> </DeliveryDate> </xsl:if> </xsl:template>
- Select Custom XSLT and click
- Click the Test Model tab. The Test Model tab with the source XML is displayed.
- In the Source XML pane, type the appropriate values for the source elements. Alternatively, you may click Fill Source Data , which assigns sample string values to the source elements. Make changes to these values, if required. The elements in the Source XML pane are populated with the specified values.
- Click Transform to start the data transformation process.
- The data is transformed and displayed in the Transformed XML pane indicating the completion of the process. The transformed XML is similar to the transformed XML in Scenario 2.
Test Source XML
<Orders> <Order> <CustomerID>GREAL</CustomerID> <date> <type>Order</type> <value>09/07/2013</value> </date> <date> <type>Billing</type> <value>09/07/2013</value> </date> <date> <type>Shipping</type> <value>09/07/2013</value> </date> </Order> </Orders> |
Generated Target XML
<PurchaseOrder> <Dates> <OrderDate>09/07/2013</OrderDate> <BillingDate>09/07/2013</BillingDate> </Dates> </PurchaseOrder> |
![]() |
The following actions on the Custom XSLT function are not allowed:
|
.